package fileby.gui.tabs;

import fileby.core.ActionKey;
import fileby.core.IDisplayableFile;
import fileby.gui.util.ContentPanel;
import fileby.core.IDisplayableFileSource;
import fileby.ctrl.actions.IActionProvider;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.ctrl.tabs.ITabContentListenerProvider;
import fileby.ctrl.tabs.TabContentListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;

/**
 *
 * @author Jacob
 */
class TabContent extends JPanel {

    private IActionProvider actionProvider;
    private ITransferHandlerProvider transferHandlerProvider;
    private ITabContentListenerProvider listenerProvider;
    private TabItemFactory tabItemFactory;
    private IDisplayableFileSource source;
    private ArrayList<TabItem> items = new ArrayList();
    private ContentPanel contents = new ContentPanel();
    private JScrollPane scroll;
    private JPopupMenu popup;

    public TabContent(IDisplayableFileSource source, IActionProvider actionProvider,
            ITransferHandlerProvider transferHandlerProvider,
            ITabContentListenerProvider listenerProvider,
            TabItemFactory tabItemFactory) {

        this.source = source;
        this.actionProvider = actionProvider;
        this.transferHandlerProvider = transferHandlerProvider;
        this.listenerProvider = listenerProvider;
        this.tabItemFactory = tabItemFactory;
        initComponents();
        initCtrl();
    }

    public void updateContent() {
       ArrayList<IDisplayableFile> newFiles = source.getFiles();
        boolean reloadAllPanels = false;
        int i = 0;
        if (newFiles.size() == items.size()) {
            for (IDisplayableFile file : newFiles) {
                if (file != items.get(i++).getDisplayableFile()) {
                    reloadAllPanels = true;
                    break;
                }
            }
        } else {
            reloadAllPanels = true;
        }
        if (reloadAllPanels) {
            contents.removeAll();
            items.clear();
            for (IDisplayableFile f : newFiles) {
                TabItem temp = tabItemFactory.create(f);
                items.add(temp);
                contents.add(temp);
            }
            contents.validate();
            contents.repaint();
            validate();
        } else {
            for (TabItem panel : items) {
                panel.update();
            }
        }
    }
    
    public void setSource(IDisplayableFileSource source) {
        this.source = source;
        updateContent();
    }

    public IDisplayableFileSource getSource() {
        return source;
    }

    private void initComponents() {
        contents.setLayout(new FlowLayout(FlowLayout.LEFT));
        contents.setOpaque(false);
        scroll = new JScrollPane(contents);
        scroll.getViewport().setBackground(Color.WHITE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        updateContent();
    }

    private void initCtrl() {
        initJPopupMenu();
        TabContentListener l = listenerProvider.create(popup);
        scroll.addMouseListener(l);
        scroll.addMouseMotionListener(l);
        setTransferHandler(transferHandlerProvider.create(TransferHandlerType.TAB_CONTENT));
    }

    private void initJPopupMenu() {
        popup = new JPopupMenu();
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.PASTE)));
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.SELECT_ALL)));
        popup.add(new JMenuItem(actionProvider.getControl(ActionKey.CREATE_FOLDER)));
    }

}
